'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')

function resolve(dir) {
  return path.join(__dirname, dir)
}

const name = defaultSettings.title || 'vue Admin Template' // page title

const port = process.env.port || process.env.npm_config_port || 9528 // dev port

module.exports = {
  publicPath: './', // 指定在什么环境下运行 /是服务器 ./ 是任何环境
  outputDir: 'dist', // 打爆目录输出的名字
  assetsDir: 'static', // 静态资源的名字
  lintOnSave: process.env.NODE_ENV === 'development', // 是否开始eslint语法检查
  productionSourceMap: false, // 是否开始map映射

  // 脚手架静态服务器相关配置
  devServer: {
    port: port, // 端口号
    open: true, // 是否自动打开浏览器
    // 是否提示
    overlay: {
      warnings: false,
      errors: true
    },
    // 开启代理服务器 解决跨域问题
    // 1.在vue.config中 配置以下配置项
    // 2. 把2request.js中把基地址改了 别写死 写成下面配置项里的'/api'
    // 3. 在api/user 中 把请求的url 中的'/api'去掉 不然会和基地址拼接 变成两个/api
    proxy: {
      '/api': {
        // 线上地址
        // target: 'http://ihrm.itheima.net'
        // 线下老师的本地地址
        // target: 'http://192.168.69.112:3000'
        target: 'http://192.168.69.222:3000'

      }
    }
  },
  // webpack 相关配置
  configureWebpack: {
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    }
  },
  chainWebpack(config) {
    // it can improve the speed of the first screen, it is recommended to turn on preload
    config.plugin('preload').tap(() => [
      {
        rel: 'preload',

        fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
        include: 'initial'
      }
    ])

    // when there are many pages, it will cause too many meaningless requests
    config.plugins.delete('prefetch')

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    config.when(process.env.NODE_ENV !== 'development', config => {
      config
        .plugin('ScriptExtHtmlWebpackPlugin')
        .after('html')
        .use('script-ext-html-webpack-plugin', [
          {
            // `runtime` must same as runtimeChunk name. default is `runtime`
            inline: /runtime\..*\.js$/
          }
        ])
        .end()
      config.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial' // only package third parties that are initially dependent
          },
          elementUI: {
            name: 'chunk-elementUI', // split elementUI into a single package
            priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
            test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
          },
          commons: {
            name: 'chunk-commons',
            test: resolve('src/components'), // can customize your rules
            minChunks: 3, //  minimum common number
            priority: 5,
            reuseExistingChunk: true
          }
        }
      })
      // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
      config.optimization.runtimeChunk('single')
    })
  }
}
